package com.self.campussecondhandmarket.controller.user;

import com.self.campussecondhandmarket.entity.domain.Carousel;
import com.self.campussecondhandmarket.entity.response.carousel.CarouselResp;
import com.self.campussecondhandmarket.resp.Result;
import com.self.campussecondhandmarket.service.CarouselService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Author: Vincent
 * <p> DateTime: 2024/4/14 22:50
 * <p> Description: 轮播图视图
 * <p>
 **/
@RestController
@RequestMapping("/api/carousel")
@CrossOrigin(origins = "http://localhost:9999", allowCredentials = "true")
public class CarouselController {
	
	@Autowired
	private CarouselService carouselService;
	
	@PostMapping("/all")
	public Result all() {
		List<CarouselResp> list = carouselService.list().stream().map(o -> {
			CarouselResp vo = new CarouselResp();
			vo.setPicUrl(o.getImg());
			return vo;
		}).toList();
		return Result.success(list);
	}
	@PostMapping("/admin/all")
	public Result adminAll() {
		List<Carousel> list = carouselService.list();
		return Result.success(list);
	}
	
	@PostMapping("/add")
	public Result add(@RequestBody Carousel carousel) {
		carouselService.save(carousel);
		return Result.success();
	}
	
	@PostMapping("/del/{id}")
	public Result del(@PathVariable Integer id) {
		carouselService.removeById(id);
		return Result.success();
	}
}
